#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2
import json
import sys
import re

reload(sys)
sys.setdefaultencoding('utf8')

weixin_token = 'shitouren'
weixin_appid = 'wxbc10fc8f17652407'
weixin_secret = '94d2c225543345a52031af10df38b805'
weixin_encodingaeskey = 'cuhUaLsKHVDRol3kP3yrvsO6tFCXi16xz5Czr2JkFaJ'
weixin_subscribe_text = u'''
么么哒！萌萌哒！啦啦啦！啪啪啪！你终于还是关注了我。从此以后，你走过的每个城市我都会陪着你。
'''

patt_ms = [
    re.compile('美食'),
    re.compile('吃货'),
    re.compile('小吃'),
]

patt_jd = [
    re.compile('景点'),
    re.compile('玩'),
]

news_ms = [
    {
        'title': '海鲜------品种丰富 个头肥大 新鲜生猛',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdcgS5xEZia3yTv4ZaKKESZrU3qmgVpNcicTbYGv44va7U9tnJdrLXvTsQ/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=1&sn=bb77bfaec43128afb38b865bb2ceedb9#rd',
    },
    {
        'title': '特色菜------天然无污染的食材',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdOcnyOZQoWmxEYo1KocjEtWwOvs5c36nY2Gfgoicbl5YUFCaLdEGmesQ/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=2&sn=d6d0fa551ac22085be8304ab384acaee#rd',
    },
    {
        'title': '风味小吃------让人流连忘返的风味美食',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd0JWDb9qmBxSHIuwPe0YVDibooqkWN3EpzPFxTTAQxKzpFmzgyBPyjcw/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=3&sn=eef0724571ea1b54a47052902bb770a9#rd',
    },
    {
        'title': '水果------丰满多汁 酸甜可口 欢迎品尝',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdwXnNyhoCyVlt13QGACias1TVudCJk5Z00986ubY8vj2mEPH66MdtmjQ/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=4&sn=50058344fcb3a3e5bf124989cc234a22#rd',
    },
    {
        'title': '糖水------品类多 味道好 餐后好零食',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd35ickgBqo7bkvZInbyQGYgibTYrbv8GPLfwv0rEM9sjk8icdmCGtEvdKw/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290540&idx=5&sn=aa464773b8e95a719df1d48ee5d693e4#rd',
    },
]

news_jd = [
    {
        'title': '五彩滩------最适合看日出的地方',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdib7biaYSTQGx7MHKg8Lgu2S7koCnOcYshoMPEKpPYgnjnWCMkjhg0eyg/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=1&sn=1d3be3f8fb81a584f5a5cfb675227a97#rd',
    },
    {
        'title': '滴水丹屏------绝壁滴水如连珠垂挂',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd8Kax8MvJYtL03LPMzDTnb6crvIIsDBmTqTN33CDy2ooQMtp3ldr4Kg/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=2&sn=d5b362360abfc6c41298e0b0df0d1981#rd',
    },
    {
        'title': '斜阳岛------可观太阳斜照此岛全景',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdRxC4O3krduviaiasKdoWFgL2Zggj5oM6Yiaic5fAQA8tHLPNIwn7mickkXQ/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=3&sn=fe7458e0a96279d26ca071077aebf9a2#rd',
    },
    {
        'title': '石螺口------适合看日落的好地方',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jd5xCqdb9BzECR8HMxQCR92p7qegurDOEm2UAibUM3rrnE3zWK3Y8QNVQ/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200290271&idx=4&sn=8e5f902ef68403d9bba53b231e4685ae#rd',
    },
]

news_about = [
    {
        'title': '涠洲岛手绘，以及石墨设计团队',
        'description': '这是一个古老的小岛，和年轻的团队',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaia0d0x0Th1xLB0Q4GfbAVtG5ejnMYsAA7wibBJuzSRLsiaG0rfeM2YodGSnY3o8QuNoTAqbj1EPQL8A/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200289683&idx=1&sn=f012c2138fefbc4665b94f929e546520#rd',
    },
]

news_tips = [
    {
        'title': '涠洲岛之旅————海鲜篇',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdfhYsvJ2FoZS68Dpbb2qsJfQh03KgbWLZUHK8kGZF414tPmibB3XVAwA/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200321645&idx=1&sn=dbde8e6bf25ff6f0b2a00f9edc1fca78#rd',
    },
    {
        'title': '涠洲岛之旅————风景篇',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnTfvMPOEibnAickxYowMemHI1WYpfC1XobD4zUou0rzQohrobGRcMzh4g/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327693&idx=1&sn=6a4c42b2b8ffb4f5f4aba591c346252a#rd',
    },
    {
        'title': '涠洲岛之旅————住宿篇',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMagWNeWJ80RibhuHk8trk69jdebCtQibMYPppoBMnYOnAHjgjpHGPf76nTa2lx5QhOhjX5Ke4unDIfmw/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200325138&idx=1&sn=78bef3cd5850f8e38033c9fc7737de8b#rd',
    },
    {
        'title': '涠洲岛之旅————交通篇',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnueqtvjdaoIO4kh1wIzA8xW07g7qShyp6Ba6MRwXxz7iapsialGDVWUNA/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200327738&idx=1&sn=fb5e0d38ce30aa5017dc20121d0eb61f#rd',
    },
    {
        'title': '涠洲岛之旅————行程篇',
        'description': '',
        'picurl': 'http://mmbiz.qpic.cn/mmbiz/8GPJWibobMaiayZl4d1AMo0YPZsRic63JSnlbLaIzvAAMXapMxf0NuHR2zEqzw44pAeWGOesd4iaIHRA19hFfg0d0g/0',
        'url': 'http://mp.weixin.qq.com/s?__biz=MjM5MzMyMzQxNA==&mid=200279456&idx=1&sn=22040a2c5e3bf0e0c7429957e3325455#rd',
    },
]

word_to_item = [
    [patt_ms, news_ms],
    [patt_jd, news_jd],
]

event_to_item = {
    'V_001_CLICK_HOME': news_about,
    'V_001_CLICK_TIPS': news_tips,
}


def match_word(p_str):
    for pair in word_to_item:
        if find(pair[0], p_str):
            return pair[1]
    return None


def match_event(p_str):
    if event_to_item.has_key(p_str):
        return event_to_item[p_str]
    return None


def find(patt, p_str):
    res = None
    for one in patt:
        res = res or one.search(p_str)
    return res


# 自定义菜单
def createmenu():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + weixin_appid + '&secret=' + weixin_secret
    response = urllib2.urlopen(url)
    html = response.read()
    tokeninfo = json.loads(html)
    token = tokeninfo['access_token']

    #{
    #    "button":[
    #        {
    #            "type":"view",
    #            "name":"石头人家族",
    #            "url":"http://www.shitouren.com/weixin/home/index.html"
    #        }
    #   ]
    #}

    post = '''
    {
        "button":[
            {
                "type":"view",
                "name":"城邦集会",
                "url":"http://www.shitouren.com/web/topic/list"
            },
            {
                "type":"view",
                "name":"城邦热议",
                "url":"http://www.shitouren.com/web/topic/top"
            }
        ]

    }
    '''

    url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' + token
    req = urllib2.Request(url, post)
    response = urllib2.urlopen(req)
    print response.read()
    return response


# 删除菜单
def deletemenu():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + weixin_appid + '&secret=' + weixin_secret
    response = urllib2.urlopen(url)
    html = response.read()
    tokeninfo = json.loads(html)
    token = tokeninfo['access_token']
    url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=' + token
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    print response.read()
    return response


if __name__ == "__main__":
    deletemenu()
    print('-------------------------')
    createmenu()

    '''
                {
                "name":"手绘地图",
                "sub_button":[
                    {
                        "type":"view",
                        "name":"地图商城",
                        "url":"http://shop105952101.taobao.com/"
                    }
                ]
            },
    '''